package com.suitsoft.ut.util.upload;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.suitsoft.ut.service.ServiceException;

/**
 * 上传文件 工具类
 * 
 * @author Suineg
 */
public class UploadFileUtil {
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());

	/** 文件上传目录 */
	private final String UPLOAD_FOLDER = "WEB-INF/upload/";

	/** 上传路径 */
	private String uploadPath;
	/** 文件名 */
	private String fileName;
	/** WEB路径 */
	private String webPah;
	/** 本地保存文件名 */
	private String localFileName;

	public UploadFileUtil(ServletContext sc, String fileName) {
		this.fileName = fileName;
		this.webPah = sc.getRealPath(UPLOAD_FOLDER);
		this.uploadPath = webPah;
	}

	// 创建上传路径
	// d:\\www\\upload\\2012\\04\\10
	public void CreateUploadPath() {
		SimpleDateFormat fmtDD = new SimpleDateFormat("dd");
		SimpleDateFormat fmtMM = new SimpleDateFormat("MM");
		SimpleDateFormat fmtYY = new SimpleDateFormat("yyyy");

		Date date = new Date();
		String strDD = fmtDD.format(date);
		String strMM = fmtMM.format(date);
		String strYY = fmtYY.format(date);

		// DateTime timeCur = DateTime.Now;
		String timeStr = strYY + "/" + strMM + "/" + strDD;
	//	this.uploadPath = strYY + "/" + strMM + "/" + strDD + "/";
		this.uploadPath = this.uploadPath + "/" + timeStr + "/";

		File folder = new File(this.uploadPath);
		if (!folder.exists()) {
			folder.mkdirs();
		}
	}

	public String getUploadPath() {
		return  uploadPath;
	}

	/*
	 * 创建临时文件名称。 逻辑： 临时文件名称 = 远程文件名称 + tmp + 当前时间毫秒数 说明：
	 * 加当前时间毫秒是为了防止多个用户同时上传相同文件时，创建临时文件会产生冲突的问题。 返回值：
	 * D:\\webapps\\upload\\2012\\05\\24\\QQ2012.exe.tmp201205241990
	 */
	public String CreateTmpFileName() {
		SimpleDateFormat fmtDD = new SimpleDateFormat("dd");
		SimpleDateFormat fmtMM = new SimpleDateFormat("MM");
		SimpleDateFormat fmtYY = new SimpleDateFormat("yyyy");
		SimpleDateFormat fmtSS = new SimpleDateFormat("SSSS");

		Date date = new Date();
		String strDD = fmtDD.format(date);
		String strMM = fmtMM.format(date);
		String strYY = fmtYY.format(date);
		String strSS = fmtSS.format(date);

		this.localFileName = this.fileName + ".tmp" + strYY + strMM + strDD
				+ strSS;
		return this.localFileName;

	}

	public String getNewFileNamePath() {
		CreateUploadPath();
		return this.getUploadPath() + this.localFileName;
	}

	public String getLocalFileName() {
		return localFileName;
	}
	/**
	 * 上传
	 * @return
	 * @throws Exception
	 */
	public static void upload(File uploadFile,Long id,String uploadFileFileName,int f,ServletContext sc) throws Exception {
		if(uploadFile == null){
			throw new ServiceException();
		}else{
			UploadFileUtil fileUtil = new UploadFileUtil(sc,uploadFileFileName);
			fileUtil.CreateTmpFileName();
			File tempFileName = new File(fileUtil.getNewFileNamePath());
			uploadFile.renameTo(tempFileName);
			XdbFiles xdbFile = new XdbFiles(tempFileName,uploadFileFileName);
			xdbFile.setUid(Integer.valueOf(id.toString()));
			xdbFile.setMiddleDabian(f);
			UploaderDBUtil dbUtil = new UploaderDBUtil(sc);
			dbUtil.add(xdbFile);
		}
	}
}
